	package ca.etsmtl.log430.lab3;
	import java.io.PipedReader;
import java.io.PipedWriter;
import java.util.ArrayList;
import java.util.Collections;

public class TriFilter extends Thread{


		// Declarations
		ArrayList<String> list=new ArrayList<String>();
		PipedReader inputPipe = new PipedReader();
		PipedWriter outputPipe = new PipedWriter();

		public TriFilter(PipedWriter inputPipe,PipedWriter outputPipe) {
			try {
				// Connect InputPipes to upstream filters
				this.inputPipe.connect(inputPipe);
				System.out.println("FormatFilter:: connected to upstream filters."+ inputPipe);
			} catch (Exception Error) {
				System.out.println("FormatFilter:: Error connecting input pipes.");
			} // try/catch

			try {
				// Connect outputPipe to downstream filter
				this.outputPipe = outputPipe;
				System.out.println("FormatFilter:: connected to downstream filter.");
			} catch (Exception Error) {
				System.out.println("FormatFilter:: Error connecting output pipe.");
			} // catch

		} // Constructor

		// This is the method that is called when the thread is started

		public void run() {
			// Declarations
			
			boolean done;

			// Begin process data from the input pipes
			try {
				// Declarations

				// Need to be an array for easy conversion to string
				char[] characterValue = new char[1];
				// Indicate when you are done reading on pipe
				done = false;
				// integer values read from the pipes
				int integerCharacter;
				// lines of text from input pipe
				String lineOfText1 = "";
				// Indicate whether lines of text are ready to be output
				// to downstream filters
				boolean write1 = false;

				// Loop for reading data

				while (!done) {
					// Read pipe 
					if (!done) {
						integerCharacter = inputPipe.read();
						
						characterValue[0] = (char) integerCharacter;
						//System.out.println("yahoooooooooooooooooooou:"+(char) integerCharacter);
						if (integerCharacter == -1) // pipe #1 is closed
						{
							done = true;
							System.out.println("TriFilter:: Input pipe closed.");

							try {
								inputPipe.close();
							} catch (Exception Error) {
								System.out.println("TriFilter:: Error closing input pipe 1.");
							} // try/catch

						} else {

							if (integerCharacter == '\n') // end of line
							{
								System.out.println("TriFilter:: Received: "+ lineOfText1 + " on input pipe .");
								for(int i=0;i<list.size();i++){
									list.set(i, Formater(list.get(i)));	//avant de trier, je formate les chaines
								}
								trier(list);		
								//System.out.println("la ligne recue==========>"+lineOfText1);
								list.add(lineOfText1);
								write1 = true;
							} else {
								lineOfText1 += characterValue[0];
							} // if

						} // if ( IntegerCharacter1 == -1 )
						
					} // if (!Done1)
					
					

					if (write1) {
						write1 = false;
						
						try {
							System.out.println("TriFilter:: Sending "
									+ lineOfText1 + " to output pipe.");
							for(int i=0;i<list.size();i++){
								list.set(i, list.get(i)+"\n");
								outputPipe.write(list.get(i), 0, list.get(i).length());
								outputPipe.flush();
								//list.set(i, "")  ;			///a tester
							}
							//lineOfText1 += "\n";
							//outputPipe.write(lineOfText1, 0, lineOfText1.length());
						//	outputPipe.flush();
						} catch (Exception IOError) {
							System.out.println("TriFilter:: Write Error.");
						} // try/catch

						//list.set(i, "")  ;

					} // if ( Write1 )

					
				} // while ( !Done )

			} // try

			catch (Exception Error) {
				System.out.println("TriFilter:: Interrupted.");
			} // catch

			try {
				System.out.println("TriFilter:: output pipe closed.");
				outputPipe.close();
			} catch (Exception Error) {
				System.out.println("TriFilter:: Error closing pipes");
			} // try/catch

		} // run
		/**
		 * Cette methode rajoute l etat en debut de chaine pour faciliter le triage par ordre alphabetiaue
		 * @param lineOfText1
		 * @return
		 */
		private String Formater(String line){		//je vais rajouter l etat au debut de la ligne pour trier par ordre alphabetiaue
			System.out.println("oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
			String newline="";
			String[] tab=line.split(" ");
			String[] tab2=new String[tab.length+1];
			tab2[0]=tab[1];
			for(int i=0;i<tab.length;i++){
				tab2[i+1]=tab[i];
			}
			for(int i=0;i<tab2.length;i++){			//je colle dans la nouvelle chaine
				newline=tab2[i]+" ";
			}
			return newline;
		}
		/**
		 * cette methode trie la liste par ordre alphabetique
		 * @param list
		 */
		@SuppressWarnings("unchecked")
		private void trier(ArrayList list) {
			Collections.sort(list);
			
		}

} // class

